// pages/address-edit/address-edit.js
var api = require('../../api.js');
//var area_picker = require('../../area-picker/area-picker.js');
var app = getApp();
Page({
    data: {
        address_id:"",
        isEdit:false,//编辑地址 VS 新增地址
        firstname: "",
        lastname:"",
        phonenumber:"",//电话号码
        streetadress:"",
        city:"",
        province:"",//United Kingdom
        provinceList:[],
        // provinceList:['Alberta','British Columbia','Manitoba','Nouveau-Brunswick',
        //     'New Foundland','Nova Scotia','Ontario',
        //     'Prince Edward Island','Quebec','Saskatchewan',],
            // 'Yukon','Northwest Territories','Nunavut',
        postcode:"",
        isDefault:false,
        isShowPop:false,//选择省的弹窗
        // detail: "",
        // district: null,
    },
    onLoad: function (options) {
        var page = this;
        // page.getDistrictData(function (data) {
        //     area_picker.init({
        //         page: page,
        //         data: data,
        //     });
        // });

        page.setData({
            address_id: options.id,
        });
        if (options.id) {
            wx.showLoading({
                title: "正在加载",
                mask: true,
            });
            app.request({
                url: api.user.address_detail+ options.id,
                // data: {
                //     id: options.id,
                // },
                success: function (res) {
                    wx.hideLoading();
                    if (res.code == 200) {
                        let data = res.data;
                        page.setData({
                            isEdit: true,
                            address_id:data.id,
                            firstname: data.firstName,
                            lastname: data.lastName,
                            phonenumber:data.phoneNumber,
                            streetadress:data.streetAddress,
                            city:data.city,
                            province:data.province,
                            postcode:data.postalCode,
                            isDefault:data.defaultStatus==1?true:false
                        });
                    }else{
                        wx.showToast({
                            title: res.message,
                            image: "/images/icon-warning.png",
                        });
                    }
                }
            });
        }
    },
    onChange(event){
        console.log(event);
    },
    showProvincePop(){
        let page = this;
        page.setData({
            isShowPop: !page.data.isShowPop
        })
    },
    selectProvince(event){
        let page = this;
        const { picker, value, index } = event.detail;
        console.log(value);
        page.setData({
            province:value,
            isShowPop: false
        })
    },
    cancleselectProvince(){
        let page = this;
        page.setData({
            isShowPop: false
        })
    },
    getDistrictData: function (cb) {
        var district = wx.getStorageSync("district");
        if (!district) {
            wx.showLoading({
                title: "正在加载",
                mask: true,
            });
            app.request({
                url: api.default.district,
                success: function (res) {
                    wx.hideLoading();
                    if (res.code == 200) {
                        district = res.data;
                        wx.setStorageSync("district", district);
                        cb(district);
                    }
                }
            });
            return;
        }
        cb(district);
    },

    onAreaPickerConfirm: function (e) {
        //console.log(e);
        var page = this;
        page.setData({
            district: {
                province: {
                    id: e[0].id,
                    name: e[0].name,
                },
                city: {
                    id: e[1].id,
                    name: e[1].name,
                },
                district: {
                    id: e[2].id,
                    name: e[2].name,
                },
            }
        });
    },
    onChange(name,detail){
        let page = this;
        page.setData({
            'page.data.name':detail
        })
        console.log(page.data[name])
    },
    onChangefir(event){
        this.onChange('firstname',event.detail);
        console.log(event.detail)
    },
    changeDefault(e){
        console.log(e);
        let val = e.detail;
        let page = this;
        page.setData({
            isDefault: val
        })
    },
    saveAddress: function () {
        var page = this;
        if(page.data.isEdit){

        }
        console.log(page.data);
        // var myreg = /^([0-9]{6,12})$/;
        // var myreg2 = /^(\d{3,4}-\d{6,9})$/;
        // console.log(myreg2.test(page.data.mobile));
        // if (!myreg.test(page.data.mobile) && !myreg2.test(page.data.mobile)) {
            
        // }
        if(!page.data.firstname){
            wx.showToast({
                title: "请正确填写First Name",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.lastname){
            wx.showToast({
                title: "请正确填写Last Name",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.phonenumber){
            wx.showToast({
                title: "请正确填写Phone Number",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.streetadress){
            wx.showToast({
                title: "请正确填写Street Address",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.city){
            wx.showToast({
                title: "请正确填写City",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.province){
            wx.showToast({
                title: "请正确填写Province",
                image: "/images/icon-warning.png",
            });
            return false;
        }else if(!page.data.postcode){
            wx.showToast({
                title: "请正确填写Post Code",
                image: "/images/icon-warning.png",
            });
            return false;
        }else{
            wx.showLoading({
                title: "正在保存",
                mask: true,
            });
            let finalUrl = page.data.isEdit?(api.user.address_update+page.data.address_id):api.user.address_save;
            app.request({
                url: finalUrl,
                method:"post",
                data: {
                    defaultStatus:page.data.isDefault?1:0,
                    //id: page.data.address_id || "",
                    firstName: page.data.firstname,
                    lastName: page.data.lastname,
                    phoneNumber: page.data.phonenumber,
                    streetAddress: page.data.streetadress,
                    city: page.data.city,
                    province: page.data.province,
                    postalCode: page.data.postcode,
                },
                success: function (res) {
                    wx.hideLoading();
                    if (res.code == 200) {
                        wx.showModal({
                            title: "提示",
                            content: res.message,
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    wx.navigateBack();
                                }
                            }
                        });
                    }else{
                        wx.showToast({
                            title: res.message,
                            image: "/images/icon-warning.png",
                        });
                    }
                }
            });
        }
        
    },

    inputBlur: function (e) {
        //console.log(e);
        var name = e.currentTarget.dataset.name;
        var value = e.detail;
        //var data = '{"form":{"' + name + '":"' + value + '"}}';
        var data = '{"' + name + '":"' + value + '"}';
        this.setData(JSON.parse(data));
    },

    getWechatAddress: function (e) {
        var page = this;
        wx.chooseAddress({
            success: function (e) {
                if (e.errMsg != 'chooseAddress:ok')
                    return;
                wx.showLoading();
                app.request({
                    url: api.user.wechat_district,
                    data: {
                        national_code: e.nationalCode,
                    },
                    success: function (res) {
                        if (res.code == 1) {
                            wx.showModal({
                                title: '提示',
                                content: res.msg,
                                showCancel: false,
                            });
                        }
                        page.setData({
                            name: e.userName || "",
                            mobile: e.telNumber || "",
                            detail: e.detailInfo || "",
                            district: res.data.district,
                        });
                    },
                    complete: function () {
                        wx.hideLoading();
                    }
                });
            }
        });
    },

    onReady: function () {

    },
    onShow: function () {

    },
});